
Last chance! 50% off unlimited learning
Sale ends in
This function provides global pairwise evaluations.
comm.pairwise(X, pairid.gbd = NULL,
FUN = function(x, y, ...){ return(as.vector(dist(rbind(x, y), ...))) },
..., diag = FALSE, symmetric = TRUE, comm = .pbd_env$SPMD.CT$comm)
This function returns a common matrix with 3 columns
named i
, j
, and value
. Each value
is the
returned value and computed by FUN(X[i,], X[j,])
where
(i, j)
is the global index as ordered in a distance matrix
for i-th row and j-th columns.
a common matrix across ranks, or a gbd matrix. (See details.)
a pair-wise id in a gbd format. (See details.)
a function to be evaluated for given pairs.
extra variables for FUN
.
if matching the same elements, (i, i)
for all i
.
if matching upper triangular elements. TRUE for
i >= j
only, otherwise for all (i, j)
.
a communicator number.
Wei-Chen Chen wccsnow@gmail.com, George Ostrouchov, Drew Schmidt, Pragneshkumar Patel, and Hao Yu.
This function evaluates the objective function
FUN(X[i,], X[j, ])
(usually distance of two elements)
on any given pair (i, j)
of a matrix X
.
The input X
should be in common across all ranks if pairid.gbd
is provided, e.g. from comm.pairwise()
.
i.e. X
is exactly the same in every ranks, but
pairid.gbd
is different and in gbd format indicating the row pair
(i, j)
should be evaluated. The returning gbd matrix is ordered
and indexed by pairid.gbd
.
Note that checking consistence of X
across all ranks is not
implemented within this function since that drops performance and
may be not accurate.
The input X
should be a gbd format in row major blocks
(i.e. X.gbd
) if pairid.gbd
is NULL
. A internal
pair indices will be built implicitly for evaluation. The returning
gbd matrix is ordered and indexed by X.gbd
.
Programming with Big Data in R Website: https://pbdr.org/
comm.pairwise()
, and
comm.dist()
.
if (FALSE) {
### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r
spmd.code <- "
### Initialize
suppressMessages(library(pbdMPI, quietly = TRUE))
### Examples.
comm.set.seed(123456, diff = FALSE)
X <- matrix(rnorm(10), ncol = 2)
id.matrix <- comm.allpairs(nrow(X))
### Method original.
dist.org <- dist(X)
### Method 1.
dist.common <- comm.pairwise(X, pairid.gbd = id.matrix)
### Method 2.
# if(comm.rank() != 0){
# X <- matrix(0, nrow = 0, ncol = 4)
# }
X.gbd <- comm.as.gbd(X) ### The other way.
dist.gbd <- comm.pairwise(X.gbd)
### Verify.
d.org <- as.vector(dist.org)
d.1 <- do.call(\"c\", allgather(dist.common[, 3]))
d.2 <- do.call(\"c\", allgather(dist.gbd[, 3]))
comm.print(all(d.org == d.1))
comm.print(all(d.org == d.2))
### Finish.
finalize()
"
# execmpi(spmd.code, nranks = 2L)
}
Run the code above in your browser using DataLab